package com.dldata.devtools30.controller.sysform;

import com.alibaba.fastjson.JSON;
import com.dldata.devtools30.entity.DevtFormListtemplTheaderEntity;
import com.dldata.devtools30.repository.common.DlBaseJdbcTemplate;
import com.dldata.devtools30.repository.sysform.DevtFormListtemplArgsDao;
import com.dldata.devtools30.repository.sysform.DevtFormListtemplTheaderDao;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

/**

 */
@Controller
@RequestMapping(value = "/DevtFormListTemplTheaderController")
public class DevtFormListTemplTheaderController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private DevtFormListtemplArgsDao sysCrfFormListtemplArgsDao;

    @Autowired
    private DevtFormListtemplTheaderDao sysCrfFormListtemplTheaderDao;

    @Autowired
    DlBaseJdbcTemplate dlBaseJdbcTemplate;


    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAllSysForm(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam Map<String, Object> param){


        List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
        List <DevtFormListtemplTheaderEntity> customTypeEntityList =Lists.newArrayList( sysCrfFormListtemplTheaderDao.findAll());
        customTypeEntityList.stream().filter(
            sysCrfFormListtemplTheaderEntity -> {
               return sysCrfFormListtemplTheaderEntity.getTempleid().equals(param.getOrDefault("pid", "").toString());
            }
        ).sorted(
                new Comparator<DevtFormListtemplTheaderEntity>() {
                    @Override
                    public int compare(DevtFormListtemplTheaderEntity o1, DevtFormListtemplTheaderEntity o2) {
                        return o1.getXh()-o2.getXh();
                    }
                }
        ).forEach(
                (sysCrfFormTemplateEntity) -> {
                    Map<String, Object> tmpmap = (Map<String, Object>) JSON.parseObject(JSON.toJSONString(sysCrfFormTemplateEntity));

                    mapList.add(tmpmap);
                }
        );
        return Result.ok(mapList);

    }

    @RequestMapping(value = "save")
    @ResponseBody
    public Result save(DevtFormListtemplTheaderEntity sysCrfFormEntity, HttpServletRequest request, HttpServletResponse response) throws  Exception{

        String result;
        try {
            if (sysCrfFormEntity.getId()==null||sysCrfFormEntity.getId().equals("")) {
                sysCrfFormEntity.setId(UUID.randomUUID().toString());
            }
//            sysCrfFormListtemplTheaderDao.save(sysCrfFormEntity.getId(),sysCrfFormEntity);
            sysCrfFormListtemplTheaderDao.save(sysCrfFormEntity);
            return Result.ok();

        } catch (Exception ex) {

            logger.error("【执行" + this.getClass().getName() + ".save() 方法失败 】异常：" + ex.getMessage());
            return Result.error("操作错误，请稍候再试！");
        }

    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id,HttpServletRequest request, HttpServletResponse response) throws Exception{
        String result;

        try {
            sysCrfFormListtemplTheaderDao.deleteById(id);

            return Result.ok();
        }
        catch (Exception ex){

            logger.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return Result.error("操作错误，请稍候再试！");
        }

    }



    @RequestMapping(value = "toEdit")
    public String toEdit(String id,String pid, HttpServletRequest request, HttpServletResponse response, Model model) throws  Exception{

        String result;
        try {
            DevtFormListtemplTheaderEntity sysCrfFormEntityTmp=null;
            if(!StringUtil.isEmptyOrLength0(id)){
                sysCrfFormEntityTmp=sysCrfFormListtemplTheaderDao.findById(id).get();
            }
            if(sysCrfFormEntityTmp==null){
//                List<SysFormListtemplTheaderEntity> listtemplTheaderEntities=sysCrfFormListtemplTheaderDao.getAllCloneData();
                List<DevtFormListtemplTheaderEntity> listtemplTheaderEntities= Lists.newArrayList(sysCrfFormListtemplTheaderDao.findAll());
                listtemplTheaderEntities=listtemplTheaderEntities.stream().filter(
                        sysCrfFormListtemplTheaderEntity -> {
                            return sysCrfFormListtemplTheaderEntity.getTempleid().equalsIgnoreCase(pid);
                        }
                ).collect(Collectors.toList());
                sysCrfFormEntityTmp =new DevtFormListtemplTheaderEntity();
                sysCrfFormEntityTmp.setTempleid(pid);
                sysCrfFormEntityTmp.setXh(listtemplTheaderEntities.size()+1);
                sysCrfFormEntityTmp.setWidth(100);
            }

            model.addAttribute("data",sysCrfFormEntityTmp);
        } catch (Exception ex) {
            logger.error("【执行" + this.getClass().getName() + ".save() 方法失败 】异常：" + ex.getMessage());
        }
        return "thymeleaf/devtools30/sysform/v_SysFormListTemplTheaderEdit";
    }


}
